package ru.alt22.visor.php.core.codegen;

import java.util.ArrayList;

import org.eclipse.php.internal.core.phpModel.phpElementData.PHPFunctionData;
import org.eclipse.php.internal.core.phpModel.phpElementData.PHPModifier;

public class PHPClass extends PHPIndentable {

	private int modifier;
	private String superclass;
	private ArrayList<String> interfaces;
	private ArrayList<PHPMethod> methods;
	
	public PHPClass() {
		this(0, null);
	}
	
	public PHPClass(int modifier, String name) {
		this(modifier, name, null);
	}

	public PHPClass(int modifier, String name, String superclass) {
		super(name);
		setSuperclass(superclass);
		interfaces = new ArrayList<String>();
		methods = new ArrayList<PHPMethod>();
	}

	public void setModifier(int modifier) {
		this.modifier = modifier;
	}

	public int getModifier() {
		return modifier;
	}

	public void setSuperclass(String superclass) {
		this.superclass = superclass;
	}

	public String getSuperclass() {
		return superclass;
	}

	public void addInterface(String newInterface) {
		interfaces.add(newInterface);
	}

	public ArrayList<String> getInterfaces() {
		return interfaces;
	}

	public void addMethod(PHPMethod newMethod) {
		methods.add(newMethod);
	}

	public void addMethod(int modifiers, String name, String body) {
		addMethod(new PHPMethod(modifiers, name, body));
	}

	public void addMethod(int modifiers, String name) {
		this.addMethod(modifiers, name, null);
	}

	public void addMethod(PHPFunctionData method) {
		addMethod(method.getModifiers(), method.getName());
	}

	public void addMethodParameter(String methodName, String parameterName, String type, String defaultValue) {
		for (int i = 0; i < methods.size(); i++) {
			PHPMethod method = (PHPMethod) methods.get(i);
			if (!method.getName().equalsIgnoreCase(methodName)) {
				continue;
			}
			method.addParameter(parameterName, type, defaultValue);
		}
	}

	public ArrayList<PHPMethod> getMethods() {
		return methods;
	}

	public String toString(int indent) {
		StringBuffer result = new StringBuffer();

		result.append(indent(indent) + getClassSignature() + " {\n");
		result.append("\n");
		for (int i = 0; i < getMethods().size(); i++) {
			PHPMethod method = (PHPMethod) getMethods().get(i);
			result.append(method.toString(2));
			result.append("\n");
		}
		if (result.charAt(result.length() - 1) != '\n') {
			result.append("\n");
		}
		result.append(indent(indent) + "}\n");

		return result.toString();
	}

	private String getClassSignature() {
		StringBuffer result = new StringBuffer();
		result.append(PHPModifier.toString(getModifier()));
		if (getModifier() > 0) {
			result.append(" ");
		}
		result.append("class ");
		result.append(getName());
		
		if (getSuperclass() != null && getSuperclass().length() > 0) {
			result.append(" extends ");
			result.append(getSuperclass());
		}

		if (getInterfaces().size() > 0) {
			result.append(" implements ");
			for (int i = 0; i < getInterfaces().size(); i++) {
				result.append(getInterfaces().get(i));
				if (i != getInterfaces().size() - 1) {
					result.append(", ");
				}
			}
		}
		
		return result.toString();
	}

}